package com.org.csu.converter;

public class FlowConverter {
    public double parseFlow(String flowStr) {
        if (flowStr == null || flowStr.isEmpty()) {
            return 0.0;
        }

        // 处理"<10"的情况
        if (flowStr.startsWith("<")) {
            return 5.0;
        }

        // 处理"11.7万~18.7万"的情况，计算平均值
        String[] parts = flowStr.split("~");
        if (parts.length == 2 && parts[0].endsWith("万") && parts[1].endsWith("万")) {
            try {
                String minPart = parts[0].substring(0, parts[0].length() - 1);
                String maxPart = parts[1].substring(0, parts[1].length() - 1);
                double minFlow = Double.parseDouble(minPart) * 10000;
                double maxFlow = Double.parseDouble(maxPart) * 10000;
                return (minFlow + maxFlow) / 2.0;
            } catch (NumberFormatException e) {
                // 如果转换失败，返回0或抛出异常
                e.printStackTrace();
                return 0.0;
            }
        }else if(parts.length == 2 && !parts[0].endsWith("万") && parts[1].endsWith("万")){
            String maxPart = parts[1].substring(0, parts[1].length() - 1);
            double minFlow = Double.parseDouble(parts[0]);
            double maxFlow = Double.parseDouble(maxPart) * 10000;
            return (minFlow + maxFlow) / 2.0;
        }

        // 处理"万"的情况
        if (flowStr.endsWith("万")) {
            String numberPart = flowStr.substring(0, flowStr.length() - 1);
            return Double.parseDouble(numberPart) * 10000;
        }

        // 如果格式不匹配，返回0或抛出异常
        return 0.0;
    }
}